from geopy.geocoders import Nominatim
from openai import OpenAI
import os
import csv
import time

# calling the Nominatim tool

loc = Nominatim(user_agent="getLoc")

def get_location(latitude,longitude):
    location = loc.reverse(latitude+','+longitude)
    return location

needed_location = [4,5,6,10,11,12,13,14,15,16,17,20,21,22,24,26,27,28,29,33,35,36,55,56,57,58,59,71]





if __name__ == '__main__':
    csv_file = 'Filtered Narratives for Crash Data.csv'
    with open(csv_file, 'r') as f:
        reader = csv.reader(f)
        #print first line
        title = []
        init = True
        
        for row in reader:
            query = ''
            query+='I want you to be a data recovery expert and recover the narrative data for the following crash\n'
            query+='I will give you the narrative with XXX in it and I want you to really think about what to replace the XXX with my given information\n'
            query+='I will give you the following information\n'
            content = ''

            if init:
                title = row
                init = False
                continue
            for i in needed_location:
                content += title[i] + ' : ' + row[i] + '\n'

            # client = OpenAI()
            #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
            # response = client.chat.completions.create(
            # model="o1-mini",
            # messages=[
            #     {
            #     "role": "system",
            #     "content": query
            #     },
            #     {
            #     "role": "user",
            #     "content": content
            #     }
            # ],
            # temperature=0.7,
            # max_tokens=1024,
            # top_p=1
            # )

            latitude = row[20]
            longitude = row[21]
            location = get_location(latitude,longitude)
            query += 'with the latitude and longitude the location is location : ' + str(location) + '\n'
            # query+= 'I have something implemented for you too, if the crash happend on a location that\'s different than the gps coordinates, then you may return back to me a slightly changed location,'
            # query+= 'I have a gps lookup function hooked up on my side and if that\'s the case you must return the location you want in this format: get_location(latitude,longitude) ONLY! only this line, because I will run it'
            # query+= 'I will then send you the resolved location, but the way I detect it on my side is to check if your respond contains the get_location keyword, so if you don\'t want to change the location, don\'t use it\n'
            query+= 'In the end I want you to return the narrative with as much as the XXX replaced as possible, but I also don\'t want the ambigious answer.'

            print('initial query:',query)
            print()
            print('initial content:',content)


            # while(True):
            #     try:
            #         result = OpenAI().chat(query)
            #     except:
            #         time.sleep(5)
            #         continue
            #     if 'get_location' in result:
            #         res = eval(result)
            #         query += res
            #         print('requested location:', res)
            #         print('resolved location:',query)
            #     else:
            #         break
            print(response["choices"][0]["text"])
            break

